#!/usr/bin/python
#-*- coding: utf-8 -*-

###########################################################
# Copyright 2011 Daniel 'grindhold' Brendle and Team
#
# This file is part of Kitchen of Awesome.
#
# Kitchen of Awesome is free software: you can redistribute it and/or 
# modify it under the terms of the GNU General Public License 
# as published by the Free Software Foundation, either 
# version 3 of the License, or (at your option) any later 
# version.
#
# Kitchen of Awesome is distributed in the hope that it will be 
# useful, but WITHOUT ANY WARRANTY; without even the implied 
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public 
# License along with Kitchen of Awesome. 
# If not, see http://www.gnu.org/licenses/.
###########################################################

from data import KoaObject, APPLICATION

class Note(KoaObject):
    @classmethod
    def getNotesForUser(cls,user):
        notes = APPLICATION.getStorage().find({'recipient_id':user.id, '_className':'Note'})
        return [KoaObject.load(i['_id']) for i in notes]

    @classmethod
    def createNote(cls, author, recipients, content):
        if type(recipients) == list:
            ret = []
            for recipient in recipients:
                note = Note()
                note.setAuthor(author)
                note.setRecipient(recipient)
                note.setContent(content)
                note.store()
                ret.append(note)
            return ret
        note = Note()
        note.setAuthor(author)
        note.setRecipient(recipients)
        note.setContent(content)
        note.store()
        return note

    def __init__(self):
        KoaObject.__init__(self)
        self.author = None
        self.recipient = None
        self.content = ""

    def setContent(self,content):
        self.content = content

    def getContent(self):
        return self.content

    def setAuthor(self,author):
        if author.__class__.__name__ != "User":
            raise Exception ("Must be User")
        self.author = author

    def getAuthor(self):
        return self.author

    def setRecipient(self, recipient):
        if recipient.__class__.__name__ != "User" and recipient is not None:
            raise Exception ("Must be User")
        self.recipient = recipient

    def getRecipient(self):
        return self.recipient

KoaObject.registerClass(Note)